// pages/championRoadEnroll/championRoadEnroll.js
let http = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '冠军之路',
    maskFlag: true,
    popupViewPay: true,
    popupViewEnroll: true
  },
  // 监听名字
  nameEnroll(e) {
    let nameEnroll = e.detail.value
    if (nameEnroll) {
      this.setData({
        nameEnroll
      })
    } else {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none'
      })
    }

  },
  // 监听号码
  phoneEnroll(e) {
    let phoneEnroll = e.detail.value
    if (phoneEnroll) {
      this.setData({
        phoneEnroll
      })
    } else {
      wx.showToast({
        title: '电话不能为空',
        icon: 'none'
      })
    }

  },
  // 显示遮罩及打开自定义支付弹框
  showPayPopup(e) {
    let _this = this

    http.postHttp('/create_order', {
      name: _this.data.nameEnroll,
      phone: _this.data.phoneEnroll,
      class_id: _this.data.classid,
      scene: 4
    }, {
      token: wx.getStorageSync('token')
    }).then(res => {
      console.log('获取依据',res)
      this.setData({
        appId: res.data.data.appId,
        nonceStr: res.data.data.nonceStr,
        packageInfo: res.data.data.package,
        paySign: res.data.data.paySign,
        signType: res.data.data.signType,
        timeStamp: res.data.data.timeStamp
      })
    })
    this.setData({
      maskFlag: false,
      popupViewPay: false,

    })
  },
  //弹出支付请求
  showPaySuccess(e) {
    let _this = this
    wx.requestPayment({
      timeStamp: _this.data.timeStamp,
      nonceStr: _this.data.nonceStr,
      package: _this.data.packageInfo,
      signType: _this.data.signType,
      paySign:_this.data.paySign,
      success (res) {
        console.log(res,'支付成功')
        _this.setData({
          maskFlag:false,
          popupViewPay:true,
          popupViewEnroll:false
        })
       },
      fail (res) { 
        wx.showToast({
          title: '支付失败',
          icon:'none'
        })
        console.log(res,'支付失败')
      }
    })
  },
  goBackIndex(e) {
    wx.redirectTo({
      url: '/pages/myIndex/myIndex',
    })
  },
  closePopop(e) {
    this.setData({
      maskFlag: true,
      popupViewPay: true,
      popupViewEnroll: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let classid = options.classid
    let money=options.money
    this.setData({
      classid,
      money
    })
    console.log(classid,money,'options')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})